package com.csc.mtlbatch.jde.cls;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.csc.dataaccess.dao.LjdeexttrtDao;
//import com.csc.lifeasia.hibernate.pojo.Ljdeexttrt;
import com.csc.dataaccess.entity.Ljdeexttrt;
import com.csc.util.HibernateUtil;

public class Ct086{
	
	static Properties prop = new Properties();
	static InputStream input = null;
	static String fileout = "";
	
	private List<Ljdeexttrt> ljdList = null;

	private static final Logger LOGGER = LoggerFactory.getLogger(Ct086.class);
	
	public Boolean writing() {

		String strfileName  = "";
		String strDateStamp  = "";
		String strTimeStamp  = "";
		Boolean retWrite = false;
		
	    try {
	    	LOGGER.info("init Ct086");
	    	//fileout = prop.getProperty("FILEOUTPATH");
	    	//fileout = "Z:\\";
	    	fileout = new File(".").getCanonicalPath() + "\\fileout\\";
	    	
	    	this.checkPath(fileout);
	    	
	    	strDateStamp = this.getDateStampFormat();	
	    	strTimeStamp = this.getTimeStampFormat();
	    	    	
	    	strfileName = this.getfileName(strDateStamp,strTimeStamp);
	    	
	    	LOGGER.info("File Path : " + fileout + strfileName);
			
	    	ljdList = this.getAllLjdeexttrtValues();
	    	
	    	File statText = new File(fileout + strfileName);
	        FileOutputStream is = new FileOutputStream(statText);
	        OutputStreamWriter osw = new OutputStreamWriter(is);    
	        BufferedWriter w = new BufferedWriter(osw);
	    	
	        w.write(this.getLjdHeaderFormat(strfileName, strDateStamp));
	        
	    	for (Ljdeexttrt next : ljdList) {
	    		w.newLine();
	    		w.write(this.getLjdDetailFormat(next));
	    	}
	    	
	    	w.newLine();
	    	w.write(this.getLjdFooterFormat(ljdList));
	    	LOGGER.info("File " + strfileName + " Generated.");
	        w.close();
	        retWrite = true;
	    } catch (IOException e) {
	        LOGGER.error("Problem writing to the file.");
	        e.printStackTrace();
	        retWrite = false;
	    } catch (Exception ex) {
	        LOGGER.error("Problem writing to the file.");
	        ex.printStackTrace();
	        retWrite = false;
	    }
	    
	    return retWrite;
	}
	
	private void checkPath(String path){
		
		File theDir = new File(path);

	  // if the directory does not exist, create it
	  if (!theDir.exists()) {
	    //System.out.println("creating directory: " + directoryName);
	    boolean result = false;

	    try{
	        theDir.mkdir();
	        result = true;
	     } catch(SecurityException se){
	        //handle it
	     }        
	     if(result) {    
	       //System.out.println("DIR created");  
	       LOGGER.info("DIR " + path + " created");
	     }
	  }
	}
	
	private List<Ljdeexttrt> getAllLjdeexttrtValues() {

		LjdeexttrtDao ljdeexttrtDao = new LjdeexttrtDao();
		List<Ljdeexttrt> LjdeList = ljdeexttrtDao.getAllLjdeexttrt();
		return LjdeList;
	}
	
	private String getfileName(String dateStamp, String timeStamp){
		return "IL_JDE_ORDUTR_" + dateStamp + timeStamp + ".dat";
	}
	
	private String getDateStampFormat(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date now = new Date();
	    String strDate = sdf.format(now);
		return strDate;
	}
	
	private String getTimeStampFormat(){
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSS");
		Date now = new Date();
	    String strTime = sdf.format(now);
		return strTime;
	}
	
	private String getLjdHeaderFormat(String fileName, String dateStamp){
		return "HEADER" + "," + fileName + "," + dateStamp;
	}
	
	private static String getLjdDetailFormat(Ljdeexttrt data){
		String retData = "";
		
		retData = checkNull(data.getTrcoco()) + ","
				+ checkNull(data.getTrtena()) + ","
				+ data.getTrtenu() + ","
				+ data.getTrtrsd() + ","
				+ data.getTrtrst() + ","
				+ data.getTrtrse() + ","
				+ checkNull(data.getTrdico()) + ","
				+ checkNull(data.getTrbanu()) + ","
				+ checkNull(data.getTrtrna()) + ","
				+ checkNull(data.getTrinit()) + ","
				+ checkNull(data.getTrreco()) + ","
				+ checkNull(data.getTrmess());
		
		return retData;
	}
	
	private static String checkNull(String strData){
			if (strData == null){
				strData = "";
			}
		return strData;
	}
	
	private String getLjdFooterFormat(List<Ljdeexttrt> dataList){
		int rowSize = dataList.size();
		rowSize = rowSize + 1;
		return "FOOTER" + "," + rowSize;
	}

	public static void main(String[]args) {
		Ct086 write = new Ct086();
	    write.writing();

		//System.out.println(write.getTimeStampFormat());
	}
	
	
}
